(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);
/**
 * This function only works if parent element has a height set
 */
(function($){
	 $.fn.extend({
	  	verticalAlign : function(options) {
		  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
			  return this.each(function() {
				  //parheight = $(this).parent().height();
				  if(options != null){
					  if(  options.height != null || options.height != 'undefined' ){
						  parheight = options.height;
					  }
				  }
				  else {
					   parheight = $(this).parent().height();
				  }
				  myheight = $(this).height();
				  console.log(myheight);
				  console.log(myheight);
				  adjust = ( parheight - myheight )/2;
				  $(this).css('position','relative');
				  $(this).css('top',adjust+'px');
			  });
		  }
		}
	 });
	})(jQuery);
(function($){
	 $.fn.extend({
	  	autoSubmit : function() {
		  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
			  return this.each(function() {
				  $(this).change( function(){
					  $(this).closest("form").submit();
				  });

			  });
		  }
		}
	 });
	})(jQuery);
/**
 * truncate text to managable lengths
 */
(function($){
	$.fn.Truncate = function(options) {
		return this.each(function() {
			text = $(this).text();
			finalStr = text.substr( 0,options.length );
			ctr = options.length;
			spaceout = options.spaceout;
			if( text.length > options.length){
				while( ctr < text.length || spaceout == 0 ){
					nChar = text.charAt(ctr);
					console.log( spaceout);
					if( nChar == ' ' ){
						finalStr+="..";
						$(this).text(finalStr);
						break;
					}
					finalStr+=nChar;
					ctr++;
					spaceout--;
				}
			}
		});
	};
})(jQuery);
$(function() {
	    focusdisable=false;
		$( "input:submit,input[type=file], a, button", ".nicebutton" ).button();
		$("input:text.niceform,textarea.niceform").css('color','#666666');
		$("input:text.niceform,textarea.niceform").hover( 
				function(){
					if( !focusdisable ){
						$(this).css('border-color','#757575');
						$(this).css('color','#000000');
					}
					
					},
				function(){
						if( !focusdisable ){
						$(this).css('border-color','#D1D1D1');
						$(this).css('color','#666666');
						}
					}
				);
		//$("input:text.niceform,textarea.niceform").css('color','#cccccc');
		// adds focus in / out border colour effect
		$("input:text.niceform,textarea.niceform").focusin(
			
				function(){
			focusdisable = true;
			$(this).css('border-color','#61C0E0');
			$(this).css('color','#000000');
				}
			);
		$("input:text.niceform,textarea.niceform").focusout(
				function(){
			focusdisable = false;
			$(this).css('border-color','#D1D1D1');
			$(this).css('color','#666666');
				}
			);
		// style any select boxes
		$('select.niceform').parent().css('margin-top','10px');
		$('select.niceform').parent().css('margin-bottom','10px');
		$('select.niceform option').css('position','absolute');
		$('select.niceform option').css('left','20px');
		$('select.niceform').customStyle();
		$(".customStyleSelectBoxInner").focus(
				function(){
			$(this).css('border-color','#61C0E0');
				},
				function(){
					$(this).css('border-color','#D1D1D1');
						}
			);
	});

function openDialog(title,msg,cancelable,action){
	var cancelHTML = "";
	var msgHTML = msg;
	var titleHTML = title;
	if( cancelable){
		cancelHTML = "<input type='button' class='pop_up' id='popup_dialog_cancel' value='Cancel'>";
	}
	$('body').append("<div id='popup_dialog' class='dialog'><div class='dialog_title'><h4>"+titleHTML+"</h4></div><div class='dialog_msg'>"+msgHTML+"</div>	<div class='pop_upcontrol'>	"+cancelHTML+"<input type='submit' class='pop_up' id='popup_dialog_ok' value='OK'></div></div>");
	$('#popup_dialog').lightbox_me({
        centered: true, 
        closeClick:false
        });
	$('#popup_dialog_cancel').click(function(e){
		$('#popup_dialog').trigger('close');
		return false;
	  });
	if( action instanceof Function ){
		$('#popup_dialog_ok').click(function(e){
			$('#popup_dialog').trigger('close');
			action.apply();
			return false;
		  });
	}
}